using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for BaseTemplate
/// </summary>
public class BaseTemplate : Acms.AcmsControls.TemplatePage
{
    public BaseTemplate()
    {
        //
        // TODO: Add constructor logic here     
        //
    }

    protected override void OnPreInit(EventArgs e)
    {
        string theme = null;
        foreach (string key in this.Request.Form.AllKeys)
        {
            if (key != null && key.StartsWith("SelectTheme"))
            {
                if (key.StartsWith("SelectTheme1"))
                {
                    theme = "Theme1";
                    break;
                }
                else if (key.StartsWith("SelectTheme2"))
                {
                    theme = "Theme2";
                    break;
                }
                else if (key.StartsWith("SelectTheme3"))
                {
                    theme = "Theme3";
                    break;
                }
                else if (key.StartsWith("SelectTheme4"))
                {
                    theme = "Theme4";
                    break;
                }
            }
        }
        if (theme != null)
        {
            HttpCookie tmp = new HttpCookie("Theme");
            tmp.Value = theme;
            Response.Cookies.Add(tmp);
        }
        else
        {
            if (Request.Cookies["Theme"] != null)
            {
                theme = Request.Cookies["Theme"].Value;
            }
        }

        //if theme not set or not equal to allowed value set to theme1
        if (theme != "Theme1" && theme != "Theme2" && theme != "Theme3" && theme != "Theme4")
        {
            theme = "Theme1";
        }

        this.Theme = theme;

        base.OnPreInit(e);
    }

    protected void SetFlashHeaderVariables(Acms.AcmsControls.FlashContentControlDisplay flashControl)
    {
        flashControl.Variables.Add("SiteRoot", string.Concat("http://", AcmsContext.SiteRoot));

        if (AcmsContext.CurrentPost != null)
        {
            Acms.Bll.PostControl headerImageControl = AcmsContext.CurrentPost.FindPostControl("Header Image");
            if (headerImageControl != null)
            {
                Acms.AcmsControls.ImageContentControlProperties headerImageProperties = headerImageControl.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                if (headerImageProperties != null && headerImageProperties.ResourceId > 0)
                {
                    flashControl.Variables.Add("headerImage", string.Format("File.aspx?Id={0}&Width={1}&Height={2}", headerImageProperties.ResourceId, 800, 374));
                }
            }
            Acms.Bll.PostControl headerLinkControl = AcmsContext.CurrentPost.FindPostControl("Header Link");
            if (headerLinkControl != null)
            {
                Acms.AcmsControls.TextContentControlProperties headerLinkProperties = headerLinkControl.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
                if (headerLinkProperties != null && headerLinkProperties.Text != null)
                {
                    string tmpUrl = headerLinkProperties.Text.Trim();
                    if (tmpUrl.StartsWith("~/"))
                    {
                        tmpUrl = tmpUrl.Substring(2);
                    }
                    else if (tmpUrl.StartsWith("/"))
                    {
                        tmpUrl = tmpUrl.Substring(1);
                    }
                    flashControl.Variables.Add("headerLink", tmpUrl);
                }
            }
            Acms.Bll.PostControl headerPrizeControl = AcmsContext.CurrentPost.FindPostControl("Header Prize");
            if (headerPrizeControl != null)
            {
                Acms.AcmsControls.TextContentControlProperties headerPrizeProperties = headerPrizeControl.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
                if (headerPrizeProperties != null && headerPrizeProperties.Text != null)
                {
                    flashControl.Variables.Add("headerPrize", headerPrizeProperties.Text.Trim());
                }
            }
        }
    }
}